home *** CD-ROM | disk | FTP | other *** search
/ Revista do CD-ROM 124 / cd-rom 124.iso / edu / tuxmath / tuxmathscrabble / tuxmathscrabble.exe / asymptopia / Validator.pyo (.txt) < prev   
Encoding:
Python Compiled Bytecode  |  2003-11-16  |  4.8 KB  |  279 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyo (Python 2.2)
  3.  
  4. import os
  5. import pygame
  6. from pygame.locals import *
  7.  
  8. class Validator:
  9.     
  10.     def __init__(self, board, game):
  11.         self.board = board
  12.         self.game = game
  13.  
  14.     
  15.     def validate(self, submission):
  16.         if len(submission) == 0:
  17.             return 0
  18.         
  19.         board = self.board
  20.         MinM = board.M
  21.         MinN = board.N
  22.         for spot in submission:
  23.             if spot.M <= MinM and spot.N <= MinN:
  24.                 MinM = spot.M
  25.                 MinN = spot.N
  26.             
  27.         
  28.         try_above = 1
  29.         while 1:
  30.             if MinN == 0:
  31.                 break
  32.             elif board.get_spotMN(MinM, MinN - 1).guest != None:
  33.                 MinN = MinN - 1
  34.                 try_above = 0
  35.             else:
  36.                 break
  37.         while 1:
  38.             if not try_above:
  39.                 break
  40.             elif MinM == 0:
  41.                 break
  42.             elif board.get_spotMN(MinM - 1, MinN).guest != None:
  43.                 MinM = MinM - 1
  44.             else:
  45.                 break
  46.         MaxM = 0
  47.         MaxN = 0
  48.         for spot in submission:
  49.             if spot.M >= MaxM and spot.N >= MaxN:
  50.                 MaxM = spot.M
  51.                 MaxN = spot.N
  52.             
  53.         
  54.         try_below = 1
  55.         while 1:
  56.             if MaxN == board.N - 1:
  57.                 break
  58.             elif board.get_spotMN(MaxM, MaxN + 1).guest != None:
  59.                 MaxN = MaxN + 1
  60.                 try_below = 0
  61.             else:
  62.                 break
  63.         while 1:
  64.             if not try_below:
  65.                 break
  66.             elif MaxM == board.M - 1:
  67.                 break
  68.             elif board.get_spotMN(MaxM + 1, MaxN).guest != None:
  69.                 MaxM = MaxM + 1
  70.             else:
  71.                 break
  72.         if MaxM - MinM > 0 and MaxN - MinN > 0:
  73.             return 0
  74.         elif MaxM - MinM > 0:
  75.             exprtype = 'col'
  76.         elif MaxN - MinN > 0:
  77.             exprtype = 'row'
  78.         elif MaxN - MinN == 0:
  79.             return 0
  80.         else:
  81.             return 0
  82.         if exprtype == 'row':
  83.             for idx in range(MinN, MaxN + 1):
  84.                 this_idx_okay = 0
  85.                 for spot in submission:
  86.                     if spot.N == idx:
  87.                         this_idx_okay = 1
  88.                     
  89.                 
  90.                 if board.get_spotMN(MinM, idx).guest != None:
  91.                     this_idx_okay = 1
  92.                 
  93.                 if this_idx_okay == 0:
  94.                     return 0
  95.                 
  96.             
  97.         elif exprtype == 'col':
  98.             for idx in range(MinM, MaxM + 1):
  99.                 this_idx_okay = 0
  100.                 for spot in submission:
  101.                     if spot.M == idx:
  102.                         this_idx_okay = 1
  103.                     
  104.                 
  105.                 if board.get_spotMN(idx, MinN).guest != None:
  106.                     this_idx_okay = 1
  107.                 
  108.                 if this_idx_okay == 0:
  109.                     return 0
  110.                 
  111.             
  112.         
  113.         expr = ''
  114.         if exprtype == 'row':
  115.             for idx in range(MinN, MaxN + 1):
  116.                 if board.get_spotMN(MinM, idx).guest != None:
  117.                     expr = expr + board.get_spotMN(MinM, idx).guest.str_val
  118.                     continue
  119.                 else:
  120.                     for spot in submission:
  121.                         if spot.N == idx:
  122.                             expr = expr + spot.guest.str_val
  123.                             continue
  124.                         
  125.                     
  126.             
  127.         elif exprtype == 'col':
  128.             for idx in range(MinM, MaxM + 1):
  129.                 if board.get_spotMN(idx, MinN).guest != None:
  130.                     expr = expr + board.get_spotMN(idx, MinN).guest.str_val
  131.                 else:
  132.                     for spot in submission:
  133.                         if spot.M == idx:
  134.                             expr = expr + spot.guest.str_val
  135.                         
  136.                     
  137.             
  138.         
  139.         if expr.count('=') == 0:
  140.             return 0
  141.         
  142.         for even_value in [
  143.             0,
  144.             2,
  145.             4,
  146.             6,
  147.             8,
  148.             10,
  149.             12,
  150.             14,
  151.             16,
  152.             18,
  153.             20]:
  154.             length = len(expr)
  155.             if expr.count('10'):
  156.                 length = length - expr.count('10')
  157.             
  158.             if expr.count('11'):
  159.                 length = length - expr.count('11')
  160.             
  161.             if expr.count('12'):
  162.                 length = length - expr.count('12')
  163.             
  164.             if expr.count('13'):
  165.                 length = length - expr.count('13')
  166.             
  167.             if expr.count('14'):
  168.                 length = length - expr.count('14')
  169.             
  170.             if expr.count('15'):
  171.                 length = length - expr.count('15')
  172.             
  173.             if expr.count('20'):
  174.                 length = length - expr.count('20')
  175.             
  176.             if length == even_value:
  177.                 return 0
  178.             
  179.         
  180.         expr = expr.replace('=', '==')
  181.         
  182.         try:
  183.             rval = eval(expr)
  184.         except:
  185.             return 0
  186.  
  187.         if rval == 0:
  188.             return 0
  189.         
  190.         head_spot = None
  191.         for spot in submission:
  192.             if spot.M == MinM and spot.N == MinN:
  193.                 head_spot = spot
  194.             
  195.         
  196.         if not head_spot:
  197.             head_spot = board.get_spotMN(MinM, MinN)
  198.         
  199.         head_spot.AMHEAD = 1
  200.         if exprtype == 'row':
  201.             head_spot.AMROWEXPR = 1
  202.             head_spot.ROWEXPRLENGTH = (MaxN - MinN) + 1
  203.         
  204.         if exprtype == 'col':
  205.             head_spot.AMCOLEXPR = 1
  206.             head_spot.COLEXPRLENGTH = (MaxM - MinM) + 1
  207.         
  208.         if exprtype == 'row':
  209.             multiplier = 1
  210.             score = 0
  211.             for nidx in range(MinN, MaxN + 1):
  212.                 spot2check = board.get_spotMN(MinM, nidx)
  213.                 if spot2check.TYPE == '2XL':
  214.                     score = score + 2
  215.                 elif spot2check.TYPE == '2XW':
  216.                     score = score + 1
  217.                     multiplier = multiplier * 2
  218.                 elif spot2check.TYPE == '3XL':
  219.                     score = score + 3
  220.                 elif spot2check.TYPE == '3XW':
  221.                     score = score + 1
  222.                     multiplier = multiplier * 3
  223.                 else:
  224.                     score = score + 1
  225.             
  226.             score = score * multiplier
  227.             self.game.playerscore = self.game.playerscore + score
  228.             for n in range(MinN + 1, MaxN + 1):
  229.                 if board.check4guest(MinM, n):
  230.                     spot2check = board.get_spotMN(MinM, n)
  231.                     if spot2check.AMHEAD and spot2check.AMROWEXPR:
  232.                         spot2check.AMROWEXPR = 0
  233.                         spot2check.ROWEXPRLENGTH = 0
  234.                         if spot2check.AMCOLEXPR:
  235.                             pass
  236.                         else:
  237.                             spot2check.AMHEAD = 0
  238.                     
  239.                 
  240.             
  241.         
  242.         if exprtype == 'col':
  243.             multiplier = 1
  244.             score = 0
  245.             for midx in range(MinM, MaxM + 1):
  246.                 spot2check = board.get_spotMN(midx, MinN)
  247.                 if spot2check.TYPE == '2XL':
  248.                     score = score + 2
  249.                 elif spot2check.TYPE == '2XW':
  250.                     score = score + 1
  251.                     multiplier = multiplier * 2
  252.                 elif spot2check.TYPE == '3XL':
  253.                     score = score + 3
  254.                 elif spot2check.TYPE == '3XW':
  255.                     score = score + 1
  256.                     multiplier = multiplier * 3
  257.                 else:
  258.                     score = score + 1
  259.             
  260.             score = score * multiplier
  261.             self.game.playerscore = self.game.playerscore + score
  262.             for m in range(MinM + 1, MaxM + 1):
  263.                 if board.check4guest(m, MinN):
  264.                     spot2check = board.get_spotMN(m, MinN)
  265.                     if spot2check.AMHEAD and spot2check.AMCOLEXPR:
  266.                         spot2check.AMCOLEXPR = 0
  267.                         spot2check.COLEXPRLENGTH = 0
  268.                         if spot2check.AMROWEXPR:
  269.                             pass
  270.                         else:
  271.                             spot2check.AMHEAD = 0
  272.                     
  273.                 
  274.             
  275.         
  276.         return 1
  277.  
  278.  
  279.